package com.c201801020208.ybl.Company.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;

import android.app.Dialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;
import android.widget.TextView;

import android.widget.Toast;

import com.c201801020208.ybl.R;

import com.c201801020208.ybl.Tables.CompanyInfo;

import cn.bmob.v3.exception.BmobException;

import cn.bmob.v3.listener.SaveListener;

public class AddJobActivity extends AppCompatActivity {

    CompanyInfo companyInfo = new CompanyInfo();
    EditText textView1;
    TextView textView2;
    EditText textView3;
    TextView textView4;
    EditText textView5;
    TextView textView6;
    TextView textView7;
    EditText textView8;
    EditText textView9;
    String Username;
    Button button;
    ImageView imageView;

    private final static int DIALOG = 1;
    boolean[] flags1 = new boolean[]{false, false, false, false, false, false, false};//初始复选情况
    boolean[] flags2 = new boolean[]{false, false, false, false, false, false, false, false};
    boolean[] flags3 = new boolean[]{false, false, false, false, false, false, false, false};
    boolean[] flags4 = new boolean[]{false, false, false, false, false, false, false};
    String[] items = null;
    String[] items2 = null;
    String[] items3 = null;
    String[] items4 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_job);
        textView1 = findViewById(R.id.editText3);
        textView2 = findViewById(R.id.editText4);
        textView3 = findViewById(R.id.editText5);
        textView4 = findViewById(R.id.editText6);
        textView5 = findViewById(R.id.editText7);
        textView6 = findViewById(R.id.editText8);
        textView7 = findViewById(R.id.editText9);
        textView8 = findViewById(R.id.editText11);
        textView9 = findViewById(R.id.editText10);
        items = getResources().getStringArray(R.array.type);
        items2 = getResources().getStringArray(R.array.xueli);
        items3 = getResources().getStringArray(R.array.salary);
        items4 = getResources().getStringArray(R.array.hours);


        Username = getIntent().getStringExtra("username");
        imageView = findViewById(R.id.imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                companyInfo.setUsername(Username);
                companyInfo.setCompanyname(textView1.getText().toString());
                companyInfo.setType(textView2.getText().toString());
                companyInfo.setJobname(textView3.getText().toString());
                companyInfo.setMoney(textView4.getText().toString());
                companyInfo.setWorkexperience(textView5.getText().toString());
                companyInfo.setEducation(textView6.getText().toString());
                companyInfo.setAddress(textView8.getText().toString());
                companyInfo.setWorktime(textView7.getText().toString());
                companyInfo.setDescribe(textView9.getText().toString());
                companyInfo.save(new SaveListener<String>() {
                    @Override
                    public void done(String objectId, BmobException e) {
                        if (e == null) {
//                            Toast.makeText(AddJobActivity.this, "添加数据成功，返回objectId为：" + objectId, Toast.LENGTH_LONG);
                            finish();
                        } else {
//                            Toast.makeText(AddJobActivity.this, "创建数据失败：" + e.getMessage(), Toast.LENGTH_LONG);
                        }
                    }
                });
            }
        });
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(1);
            }
        });
        textView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(2);
            }
        });
        textView6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(3);
            }
        });

        textView7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(4);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {

        Dialog dialog = null;
        switch (id) {
            case 1:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setIcon(R.drawable.type);
                builder.setTitle("行业类别");
                builder.setMultiChoiceItems(items, flags1, new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        flags1[which] = isChecked;
                        String results = "";
                        for (int i = 0; i < flags1.length; i++) {
                            if (flags1[i]) {
                                results = results + items[i];
                            }
                        }
                        textView2.setTextSize(20);
                        textView2.setText(results);
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog = builder.create();
                break;

            case 2:
                AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
                builder1.setIcon(R.drawable.salarys);
                builder1.setTitle("薪资要求");
                builder1.setMultiChoiceItems(items3, flags3, new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        flags3[which] = isChecked;
                        String results = "";
                        for (int i = 0; i < flags3.length; i++) {
                            if (flags3[i]) {
                                results = results + items3[i];
                            }
                        }
                        textView4.setTextSize(20);
                        textView4.setText(results);
                    }
                });
                builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog = builder1.create();
                break;

            case 3:
                AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
                builder2.setIcon(R.drawable.xueli);
                builder2.setTitle("最高学历");
                builder2.setMultiChoiceItems(items2, flags2, new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        flags2[which] = isChecked;
                        String results = "";
                        for (int i = 0; i < flags2.length; i++) {
                            if (flags2[i]) {
                                results = results + items2[i];
                            }
                        }
                        textView6.setTextSize(20);
                        textView6.setText(results);
                    }
                });
                builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog = builder2.create();
                break;

            case 4:
                AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
                builder3.setIcon(R.drawable.hours);
                builder3.setTitle("工作时间");
                builder3.setMultiChoiceItems(items4, flags4, new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        flags4[which] = isChecked;
                        String results = "";
                        for (int i = 0; i < flags4.length; i++) {
                            if (flags4[i]) {
                                results = results + items4[i];
                            }
                        }
                        textView7.setTextSize(20);
                        textView7.setText(results);
                    }
                });
                builder3.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog = builder3.create();
                break;
            default:
                break;
        }

        return dialog;
    }

}


